package cn.dckeji.gameBox.controller;


import cn.dckeji.gameBox.common.utils.MD5;
import cn.dckeji.gameBox.common.utils.ResultUtil;
import cn.dckeji.gameBox.dto.Result;
import cn.dckeji.gameBox.entity.User;
import cn.dckeji.gameBox.service.UserService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2018-08-20
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("getUserList")
    public Map<String, Object> getUserList(Integer page, Integer limit, @RequestParam(value = "realName",required = false) String realName, @RequestParam(value = "loginName",required = false) String loginName, @RequestParam(value = "date",required = false) String date){
            Page<User> userPage = new Page<>();
        userPage.setSize(limit);
        userPage.setCurrent(page);
        EntityWrapper<User> entityWrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(realName)) {
            entityWrapper.like("realName",realName);
        }
        if(StringUtils.isNotBlank(loginName)){
            entityWrapper.like("loginName",loginName);
        }
        if(StringUtils.isNotBlank(date)){
            String starttime = date.substring(0, 11);
            String enddate = date.substring(date.length() - 10);
            entityWrapper.between("createTime",starttime+" 00:00:00",enddate+" 23:59:59");
        }
        entityWrapper.orderBy("createTime",false);
        userPage = userService.selectPage(userPage,entityWrapper);
        return ResultUtil.resultPage(userPage.getTotal(),userPage.getRecords());
    }
    /**
     * 修改密码
     * @param oldpassword
     * @param newpassword
     */
    @RequestMapping(value = "changePassword",method = RequestMethod.POST)
    @ResponseBody
    public Result changePassword(String oldpassword, String newpassword, HttpSession session){
        User user= (User) session.getAttribute("adminUser");
        if (user.getLoginPwd().equals(MD5.CreateMD5(MD5.CreateMD5(oldpassword)))) {
            user.setLoginPwd(MD5.CreateMD5(MD5.CreateMD5(newpassword)));
            boolean b = userService.updateById(user);
            if (b){
                return ResultUtil.success();
            }else {
                return ResultUtil.error("操作失败");
            }
        }
        return ResultUtil.error("原密码不正确");
    }

    /**
     * 保存角色
     * @param user
     * @return
     */
    @PostMapping("saveUser")
    public Result saveUser(User user) {
        if (user.getId() == null) {
            if(checkLoginName(user.getLoginName())){
                user.setCreateTime(new Date());
                user.setLoginPwd(MD5.CreateMD5(MD5.CreateMD5("123456")));
                user.setState(1);
                userService.insert(user);
            }else{
                return ResultUtil.error("此登录名已存在，请重新输入");
            }
        }else {
            user.setUpdateTime(new Date());
            userService.updateById(user);
        }
        return ResultUtil.success();
    }
    /**
     * 删除角色
     * @param id
     * @return
     */
    @PostMapping("deleteById")
    public Result deleteById(Integer id) {
        userService.deleteById(id);
        return ResultUtil.success();
    }

    /**
     * 检查登录名是否唯一
     * @param loginName
     * @return
     */
    public boolean checkLoginName(String loginName) {
        EntityWrapper<User> entityWrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(loginName)) {
            entityWrapper.eq("loginName",loginName);
        }
        User user=userService.selectOne(entityWrapper);
        if(user!=null){
            return false;
        }
        return true;
    }
}

